function t(t, e) {
    if (!(t instanceof e)) throw new TypeError("Cannot call a class as a function");
}

Object.defineProperty(exports, "__esModule", {
    value: !0
});

var e = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
    return typeof t;
} : function(t) {
    return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t;
}, n = function() {
    function t(t, e) {
        for (var n = 0; n < e.length; n++) {
            var o = e[n];
            o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), 
            Object.defineProperty(t, o.key, o);
        }
    }
    return function(e, n, o) {
        return n && t(e.prototype, n), o && t(e, o), e;
    };
}(), o = require("./../config.js"), i = function() {
    function i(e) {
        t(this, i), this.chatId = null, this.connectStatus = !1, this.timer = null, this.watcherList = [], 
        this.app = e, this.debug = o.SERVER_DEBUG;
    }
    return n(i, [ {
        key: "connectSocket",
        value: function() {
            var t = this;
            this.chatId = new Date().getTime(), this.SocketTask = wx.connectSocket({
                url: o.WSS_SERVER_URL,
                header: o.HEADER,
                method: "post",
                success: function(e) {
                    t.debug && console.log(e);
                },
                fail: function(e) {
                    t.debug && console.log(e);
                }
            }), wx.onSocketOpen(this.onOpen.bind(this)), wx.onSocketMessage(this.onMessage.bind(this)), 
            wx.onSocketError(this.onError.bind(this)), wx.onSocketClose(this.onClose.bind(this));
        }
    }, {
        key: "ping",
        value: function() {
            var t = this;
            this.timer = setInterval(function() {
                t.send({
                    type: "ping"
                });
            }, o.PINGINTERVAL);
        }
    }, {
        key: "close",
        value: function() {
            clearInterval(this.timer), this.SocketTask.close(), this.connectStatus = !1;
        }
    }, {
        key: "send",
        value: function(t) {
            var e = this;
            return new Promise(function(n, o) {
                e.SocketTask.send({
                    data: JSON.stringify(t),
                    success: n,
                    fail: o
                });
            });
        }
    }, {
        key: "onMessage",
        value: function(t) {
            this.debug && console.log(t);
            var e = JSON.parse(t.data), n = e.type, o = e.data, i = void 0 === o ? {} : o;
            this.$emit(n, i);
        }
    }, {
        key: "onClose",
        value: function() {
            this.close();
        }
    }, {
        key: "onError",
        value: function(t) {
            this.debug && console.log(t), this.$emit("socket_error", t), this.connectStatus = !1;
        }
    }, {
        key: "onOpen",
        value: function(t) {
            this.debug && console.log("链接成功"), this.connectStatus = !0, void 0 === this.app.globalData ? console.error("无法获取token登录失败") : this.send({
                type: "login",
                data: this.app.globalData.token
            }), this.ping();
        }
    }, {
        key: "$on",
        value: function(t, n) {
            var o = this, i = this;
            "object" === (void 0 === t ? "undefined" : e(t)) ? t.forEach(function(e) {
                i.watcherList[e] || (o.watcherList[t] = []), i.watcherList[e].push(n);
            }) : (this.watcherList[t] || (this.watcherList[t] = []), this.watcherList[t].push(n));
        }
    }, {
        key: "$emit",
        value: function(t, e) {
            var n = this, o = arguments;
            this.watcherList[t].forEach(function(t) {
                t.apply(n, o, e);
            });
        }
    } ]), i;
}();

exports.default = i;